/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Helpers;

import Hibernate.HibernateUtil;
import Entities.Enquetes;
import java.util.List;
import org.hibernate.*;

/**
 *
 * @author Erhan
 */
public class EnqueteHelper {

    Session session = null;
   
    /**
     *
     */
    public EnqueteHelper() {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    //Ophalen van alles
    /**
     *
     * @return
     */
    public List<Enquetes> getEnquetes() {
        List<Enquetes> enquetesList = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Enquetes");
            enquetesList = (List<Enquetes>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return enquetesList;
    }
        //ophalen met specifieke enqueteId
    /**
     *
     * @param eid
     * @return
     */
    public List<Enquetes> getEnquetes(int eid) {
        List<Enquetes> enquetesList = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Enquetes where " + eid);
            enquetesList = (List<Enquetes>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return enquetesList;
    }

    //Ophalen van 1
    /**
     *
     * @param id
     * @return
     */
    public Enquetes getEnquete(int id) {
        Enquetes result = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Enquetes where id="+id);
            result = (Enquetes) q.list().get(0);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

        //Ophalen van 1, met specifieke naam.
    /**
     *
     * @param id
     * @return
     */

    // get alle enquete per cursus_id
    public List<Enquetes> getEnquetesPerCursus(int id) {
        List<Enquetes> enquetesList = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Enquetes where cursus_id =" +id);
            enquetesList = (List<Enquetes>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return enquetesList;
    }


    
    public Enquetes getEnquete(String naam) {
        Enquetes result = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Enquetes where naam=" +naam);
            result = (Enquetes) q.list().get(0);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }


    // Wegschrijven
    /**
     *
     * @param enquete
     */
    public void persistEnquete(Enquetes enquete) {
        org.hibernate.Transaction tx = session.beginTransaction();
        tx.begin();
        session.persist(enquete);
        tx.commit();
    }

    // Verwijderen
    /**
     *
     * @param enquete
     */
    public void deleteEnquete(Enquetes enquete) {
        org.hibernate.Transaction tx = session.beginTransaction();
        tx.begin();
        session.delete(enquete);
        tx.commit();
    }

    // Updaten
    /**
     *
     * @param enquete
     */
    public void updateEnquete(Enquetes enquete) {
        org.hibernate.Transaction tx = session.beginTransaction();
        tx.begin();
        session.update(enquete);
        tx.commit();
    }
}